<template>
  <div class="user_select_daterange">
    <div class="calendar-body">
      <!-- 年份 月份 -->
      <section class="calendar-month">
        <span
          @click="prevMonth()"
          class="prevMonth"
        >❮</span>
        <div class="text">
          {{ getYearEn(currentMonth) }}
        </div>
        <span
          @click="nextMonth()"
          class="nextMonth"
        >❯</span>
      </section>
      <!-- 日期 -->
      <div
        class="calendar-days"
      >
        <!-- 星期 -->
        <ul class="week-header">
          <li
            v-for="(text,i) of weekHeader"
            :key="i"
            class="day"
          >
            {{ text }}
          </li>
        </ul>
        <ul
          v-for="(week,i) of days"
          :key="i"
          class="week"
        >
          <li
            v-for="(day,j) of week"
            :key="j"
            :class="{'other-month':day.prevMonth || day.nextMonth,'select':isSelect(day)}"
            @click="selectDay(day)"
            class="day"
          >
            <span>{{ day.sDay }}</span>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import Util from '../modules/calendar/util.js';

export default {
  name: 'Index',
  props: {
    selectDate: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      weekHeader: ['一', '二', '三', '四', '五', '六', '日'],
      currentDate: new Date(), // 当前日期
      lang: JE.getCookies('je-local-lang') || 'zh_CN',
    };
  },
  computed: {
    days() {
      return this.getDays();
    },
    currentMonth() {
      return Ext.Date.format(this.currentDate, 'Y年m月');
    },
  },
  methods: {
    /**
     * 获得当前月的所有日期
     */
    getDays() {
      const year = this.currentDate.getFullYear();
      const month = this.currentDate.getMonth();
      const weeks = Util.calendarWeeks(year, month);
      return weeks;
    },
    /**
     * 下月
     */
    nextMonth() {
      this.currentDate = Ext.Date.subtract(this.currentDate, Ext.Date.MONTH, -1);
    },
    /**
     * 上月
     */
    prevMonth() {
      this.currentDate = Ext.Date.subtract(this.currentDate, Ext.Date.MONTH, 1);
    },
    /**
     * 选中日期
     * @param {*} day
     */
    selectDay(day) {
      if (this.selectDate instanceof Date && Ext.Date.format(this.selectDate, 'Y-m-d') == day.sDate) {
        this.selectDate = {};
        return this.$emit('othertime', this.selectDate);
      }
      this.selectDate = Ext.Date.parse(day.sDate, 'Y-m-d');
      if (day.prevMonth) {
        this.prevMonth();
      } else if (day.nextMonth) {
        this.nextMonth();
      }
      this.$emit('othertime', this.selectDate);
    },
    /**
     * 选中标记
     * @param {*} day
     */
    isSelect(day) {
      return day.sDate == Ext.Date.format(this.selectDate, 'Y-m-d');
    },
    getYearEn(cn) {
      if (!cn) return;
      if (this.lang == 'zh_CN') return cn;
      return cn.replace('年', '/').replace('月', '');
    },
  },
};
</script>

<style lang="less">
.user_select_daterange {
  .calendar-month{
    color:#2f6bcf;
    text-align: center;
    position: relative;
    *{
      cursor: pointer;
      display: inline-block;
      padding: 4px 10px;
    }
    .text{
      width:100px;
      text-align: center;
      cursor: auto;
    }
    .today{
      position: absolute;
      right:0;
    }
  }
  .calendar-days{
    .week,.week-header{
      padding:0;
      margin: 0;
      .day{
        width:14%;
        color: #333333;
        text-align: center;
        line-height: 32px;
        list-style-type: none;
        display: inline-block;
        font-size: 12px;
      }
    }
    .week{
      .day{
        cursor: pointer;
        position: relative;
      }
      .other-month{
        color:#d0d0d0;
      }
      .today span,
      .select span{
        background: #f2f2f2;
        border-radius: 12px;
        padding: 2px 8px;
      }
      .select span{
        color: #ffffff;
        background: #2f6bcf;
      }
      .task:after{
        position: absolute;
        content: " ";
        padding: 2px;
        border-radius: 100%;
        bottom: 3px;
        left: 50%;
        margin-left: -1px;
        background: #2f6bcf;
      }
    }
  }
}
</style>
